package drawing

import org.lwjgl.util.vector.{Matrix4f, Vector3f, Vector4f}

/**
 * Created by IntelliJ IDEA.
 * User: koeppelad
 * Date: 4/28/11
 * Time: 2:15 PM
 * To change this template use File | Settings | File Templates.
 */

object PositionAndOrientation
{
  val X_AXIS = new Vector3f(1, 0, 0)
  val Y_AXIS = new Vector3f(0, 1, 0)
  val Z_AXIS = new Vector3f(0, 0, 1)
}

class PositionAndOrientation(private val transformationMatrix: Matrix4f)
{
  private val ORIGIN = new Vector4f(0, 0, 0, 1)

  def this(other: PositionAndOrientation) =
    this(Matrix4f.mul(new Matrix4f(), other.toTransformationMatrix, null))

  def this() = this(new Matrix4f())

  def x: Float = return position._1
  def y: Float = return position._2
  def z: Float = return position._3

  def position: (Float, Float, Float) =
  {
    val positionVector = Matrix4f.transform(transformationMatrix, ORIGIN, null)
    return (positionVector.x, positionVector.y, positionVector.z)
  }

  def translate[X, Y, Z](x: X, y: Y, z: Z)(implicit numX : Numeric[X],
    numY : Numeric[Y], numZ : Numeric[Z]): Unit =
  {
    val vector = new Vector3f(numX.toFloat(x), numY.toFloat(y), numZ.toFloat(z))
    transformationMatrix.translate(vector)
  }

  def rotateX[N](angle: N)(implicit numeric: Numeric[N]): Unit =
    rotate(angle, PositionAndOrientation.X_AXIS)

  def rotateY[N](angle: N)(implicit numeric: Numeric[N]): Unit =
    rotate(angle, PositionAndOrientation.Y_AXIS)

  def rotateZ[N](angle: N)(implicit numeric: Numeric[N]): Unit =
    rotate(angle, PositionAndOrientation.Z_AXIS)

  def rotate[N](angle: N, axis: Vector3f)(implicit numeric: Numeric[N]): Unit =
    transformationMatrix.rotate(numeric.toFloat(angle), axis)

  def toTransformationMatrix(): Matrix4f =
  {
    val m = new Matrix4f()
    return Matrix4f.mul(m, transformationMatrix, m)
  }
}